package com.taijihuabao.common.activititasklistener.reissuecardtasklistener;

import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.ApprovalEntity;
import com.taijihuabao.modules.entity.ApprovalHistoryEntity;
import com.taijihuabao.modules.mapper.ApprovalHistoryMapper;
import com.taijihuabao.modules.mapper.ApprovalMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 任务节点监听器
 *
 * @author Tu
 * @date 2022/1/14 16:35
 */
@Component
public class ReissueCardNodalPointListener implements TaskListener {

    /**
     * 审批任务节点名
     */
    private static final String ADMIN_TASK_NAME = "连续多级主管";
    private static final String CHECKIN_ATTACHE_TASK_NAME = "考勤专员";
    private static final String RESET_SUBMIT_TASK_NAME = "提交申请";

    @Resource
    private ApprovalMapper approvalMapper;
    @Resource
    private ApprovalHistoryMapper approvalHistoryMapper;
    @Resource
    private DeptUserMapper deptUserMapper;

    @Override
    public void notify(DelegateTask delegateTask) {
        //获取businessKey
        String businessKey = delegateTask.getExecution().getProcessInstanceBusinessKey();
        Long userId = Long.valueOf(delegateTask.getAssignee());
        String taskId = delegateTask.getId();
        //获取补卡审批单
        ApprovalEntity approvalEntity = approvalMapper.selectById(Long.valueOf(businessKey));
        //设置审批历史数据
        ApprovalHistoryEntity approvalHistoryEntity = new ApprovalHistoryEntity();
        approvalHistoryEntity.setApprovalType(approvalEntity.getApprovalType());
        approvalHistoryEntity.setApprovalCode(approvalEntity.getApprovalCode());
        approvalHistoryEntity.setApplyUserId(approvalEntity.getApplyUserId());
        approvalHistoryEntity.setApplyDeptCode(approvalEntity.getApplyDeptCode());
        approvalHistoryEntity.setApplyName(approvalEntity.getApplyName());
        approvalHistoryEntity.setApprovalUserId(userId);
        String deptCode = deptUserMapper.selectDeptByUserId(userId).get(0).getDeptCode();
        approvalHistoryEntity.setApprovalDeptCode(deptCode);
        approvalHistoryEntity.setApprovalState("ing");
        approvalHistoryEntity.setApprovalTask(taskId);
        switch (delegateTask.getName()) {
            case ADMIN_TASK_NAME:
                approvalHistoryEntity.setApprovalFlow(ADMIN_TASK_NAME);
                break;
            case CHECKIN_ATTACHE_TASK_NAME:
                approvalHistoryEntity.setApprovalFlow(CHECKIN_ATTACHE_TASK_NAME);
                break;
            case RESET_SUBMIT_TASK_NAME:
                approvalHistoryEntity.setApprovalFlow(RESET_SUBMIT_TASK_NAME);
                break;
            default:
                throw new ServiceException(ApplicationResponseCode.INTER_ERROR);
        }
        approvalHistoryMapper.insertApproval(approvalHistoryEntity);
    }

}
